﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyUIManager : MonoBehaviour
{
    private static MyUIManager instance;
    //所有UIForm的合集
    public Dictionary<string, GameObject> UIFormDictionary = new Dictionary<string, GameObject>();
    //所有弹出窗体的合集
    private Stack<GameObject> PopUIFormStack = new Stack<GameObject>();
    //普通窗体节点
    private GameObject NormalNode;
    //弹出窗体节点
    private GameObject PopUpNode;
    public static MyUIManager GetInstance()
    {
        return instance;
    }
    private void Awake()
    {
        GameObject Canvas = GameObject.Find("UI");
        //foreach(Transform child in Canvas.transform)
        //{
        //    UIFormDictionary.Add(child.name, child.gameObject);
        //}
        GameObject LivingBoard = GameObject.Find("UI/PopUp/LivingBoard");
        GameObject PickItem = GameObject.Find("UI/PopUp/PickItemUIForm");
        UIFormDictionary.Add("LivingBoard", LivingBoard);
        UIFormDictionary.Add("PickItemUIForm", PickItem);
        instance = this;
        NormalNode = GameObject.Find("UI/Normal");
        PopUpNode = GameObject.Find("UI/PopUp");

    }
    private MyUIManager() { }

    /// <summary>
    /// 打开窗体
    /// </summary>
    /// <param name="UIFormName"></param>
    public void OpenUIForm(string UIFormName)
    {
        GameObject UIForm;
        UIFormDictionary.TryGetValue(UIFormName, out UIForm);
        if (UIForm == null)
        {
            UIForm = LoadUIForm(UIFormName);
            UIFormDictionary.Add(UIFormName, UIForm);
        }
        BaseUIForm baseUIForm = UIForm.GetComponent<BaseUIForm>();
        if (baseUIForm == null)
        {
            Debug.Log("请确保窗体带有BaseUIForm组件!");
        }
        //根据不同的UIForms_Type做不同的显示处理
        switch (baseUIForm.CurrentUIType.UIForms_Type)
        {
            case UIFormType.Normal:
                UIForm.transform.position = NormalNode.transform.position;
                UIForm.transform.SetParent(NormalNode.transform);
                UIForm.SetActive(true);
                break;
            case UIFormType.PopUp:
                UIForm.transform.position = PopUpNode.transform.position;
                UIForm.transform.SetParent(PopUpNode.transform);
                UIForm.SetActive(true);
                UIForm.transform.SetAsLastSibling();
                //弹出窗体入栈
                PopUIFormStack.Push(UIForm);
                break;
        }
    }
    /// <summary>
    /// 关闭窗体
    /// </summary>
    /// <param name="UIFormName"></param>
    public void CloseUIForm(string UIFormName)
    {
        GameObject UIForm;
        UIFormDictionary.TryGetValue(UIFormName, out UIForm);
        //如果字典里有这个窗体就直接关闭
        if (UIForm == null)
        {
            Debug.Log("不存在的窗体！！");
        }
        BaseUIForm baseUIForm = UIForm.GetComponent<BaseUIForm>();
        if (baseUIForm == null)
        {
            Debug.Log("请确保窗体带有BaseUIForm组件!");
        }
        //根据不同的UIForms_Type做不同的显示处理
        switch (baseUIForm.CurrentUIType.UIForms_Type)
        {
            case UIFormType.Normal:
                CloseNormalUIForm(UIForm);
                break;
            case UIFormType.PopUp:
                ClosePopUIForm(UIForm);
                break;
        }
    }


    /// <summary>
    /// 加载窗体
    /// </summary>
    /// <param name="UIFormName"></param>
    /// <returns></returns>
    private GameObject LoadUIForm(string UIFormName)
    {
        GameObject UIForm = Instantiate(Resources.Load("UI/UIForm/"+UIFormName, typeof(GameObject)) as GameObject);
        return UIForm;
    }

    /// <summary>
    /// 关闭弹出窗体
    /// </summary>
    /// <param name="UIFormName"></param>
    private void ClosePopUIForm(GameObject UIForm)
    {
        //只能关闭弹出窗体
        if (PopUIFormStack.Peek() == UIForm)
        {
            //将关闭的窗体出栈
            PopUIFormStack.Pop();
            UIForm.SetActive(false);
        }
        else
        {
            return;
        }
    }
    /// <summary>
    /// 关闭普通窗体
    /// </summary>
    /// <param name="UIform"></param>
    private void CloseNormalUIForm(GameObject UIform)
    {
        UIform.SetActive(false);
    }
}
